<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="项目名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入项目名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="清运公司" prop="transportCompanyId">
        <el-select v-model="queryParams.transportCompanyId" placeholder="请选择清运公司" filterable clearable>
          <el-option v-for="dict in transportOptions" :key="dict.id" :label="dict.name" :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="建设单位" prop="buildingDept">
        <el-input v-model="queryParams.buildingDept" placeholder="请输入建设单位" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="结构类型" prop="structureTypeId">
        <el-select v-model="queryParams.structureTypeId" placeholder="请选择结构类型" clearable>
          <el-option v-for="dict in dict.type.structure_type_id" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否启用" prop="valid">
        <el-select v-model="queryParams.valid" placeholder="请选择是否启用" clearable>
          <el-option v-for="dict in dict.type.sys_valid" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="分类项目" prop="classproject">
        <el-select v-model="queryParams.classproject" placeholder="请选择分类项目" clearable>
          <el-option v-for="dict in dict.type.sys_valid" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="施工单位" prop="constructionDept">
        <el-input v-model="queryParams.constructionDept" placeholder="请输入施工单位" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable>
          <el-option v-for="dict in dict.type.sys_valid" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="项目负责人" prop="personInCharge">
        <el-input v-model="queryParams.personInCharge" placeholder="请输入项目负责人" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="审批状态" prop="approvalStatus">
        <el-select v-model="queryParams.approvalStatus" placeholder="请选择审批状态" clearable>
          <el-option v-for="dict in dict.type.approval_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="项目性质" prop="projectKindId">
        <el-select v-model="queryParams.projectKindId" placeholder="请选择项目性质" clearable>
          <el-option v-for="dict in dict.type.project_kind_id" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="建筑物类型" prop="buildingTypeId">
        <treeselect :style="{ width: '215px' }" v-model="queryParams.buildingTypeId" :options="buildingTypeOptions"
          :show-count="true" placeholder="请选择建筑物类型" />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker v-model="daterangeCreatedDate" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-circle-check" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['projectManage:project:approveProject']">审批</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="projectList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目名称" align="center" prop="name" width="100" show-overflow-tooltip />
      <el-table-column label="项目性质" align="center" prop="projectKindId" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.project_kind_id" :value="scope.row.projectKindId" />
        </template>
      </el-table-column>
      <el-table-column label="建筑物类型" align="center" prop="buildingTypeName" width="100" />
      <el-table-column label="结构类型" align="center" prop="structureTypeId" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.structure_type_id" :value="scope.row.structureTypeId" />
        </template>
      </el-table-column>
      <el-table-column label="所属街道" align="center" prop="belongToStreet" />
      <el-table-column label="审批状态" align="center" prop="approvalStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.approval_status" :value="scope.row.approvalStatus" />
        </template>
      </el-table-column>
      <el-table-column label="清运公司" align="center" prop="transportCompanyName" width="100" show-overflow-tooltip />
      <el-table-column label="项目地址" align="center" prop="address" width="100" show-overflow-tooltip />
      <el-table-column label="建设单位" align="center" prop="buildingDept" width="100" show-overflow-tooltip />
      <el-table-column label="监理单位" align="center" prop="supervisorDept" width="100" show-overflow-tooltip />
      <el-table-column label="建筑面积" align="center" prop="buildingArea" />
      <el-table-column label="开工日期" align="center" prop="startWorkData" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startWorkData, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="竣工日期" align="center" prop="beCompletedData" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.beCompletedData, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否启用" align="center" prop="valid">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_valid" :value="scope.row.valid" />
        </template>
      </el-table-column>
      <el-table-column label="项目当前进度" align="center" prop="schedule" />
      <el-table-column label="项目负责人" align="center" prop="personInCharge" />
      <el-table-column label="负责人电话" align="center" prop="phone" />
      <el-table-column label="分类项目" align="center" prop="classproject">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_valid" :value="scope.row.classproject" />
        </template>
      </el-table-column>
      <el-table-column label="施工单位" align="center" prop="constructionDept" width="100" show-overflow-tooltip />
      <el-table-column label="定位" align="center" prop="location">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleLocation(scope.row)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-circle-check" @click="handleApprove(scope.row)"
            v-hasPermi="['projectManage:project:approveProject']">审批</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.current" :limit.sync="queryParams.size"
      @pagination="getList" />

    <!-- 添加或修改项目对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="98px" disabled>
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入项目名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属街道" prop="belongToStreet">
              <el-input v-model="form.belongToStreet" placeholder="请输入所属街道" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="项目地址" prop="address">
          <el-input v-model="form.address" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="监理单位" prop="supervisorDept">
              <el-input v-model="form.supervisorDept" placeholder="请输入监理单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="建筑面积" prop="buildingArea">
              <el-input-number v-model="form.buildingArea" placeholder="请输入建筑面积" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开工日期" prop="startWorkData">
              <el-date-picker clearable v-model="form.startWorkData" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择开工日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="竣工日期" prop="beCompletedData">
              <el-date-picker clearable v-model="form.beCompletedData" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择竣工日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="项目当前进度" prop="schedule">
          <el-input v-model="form.schedule" type="textarea" placeholder="请输入内容" maxlength="200" />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目性质" prop="projectKindId">
              <el-select v-model="form.projectKindId" placeholder="请选择项目性质" clearable>
                <el-option v-for="dict in dict.type.project_kind_id" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分类项目" prop="classproject">
              <el-select v-model="queryParams.classproject" placeholder="请选择分类项目" clearable>
                <el-option v-for="dict in dict.type.sys_valid" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目负责人" prop="personInCharge">
              <el-input v-model="form.personInCharge" placeholder="请输入项目负责人" maxlength="10" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="负责人电话" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入电话" maxlength="11" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="结构类型" prop="structureTypeId">
              <el-select v-model="form.structureTypeId" placeholder="请选择结构类型" clearable>
                <el-option v-for="dict in dict.type.structure_type_id" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="建设单位" prop="buildingDept">
              <el-input v-model="form.buildingDept" placeholder="请输入建设单位" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="施工单位" prop="constructionDept">
              <el-input v-model="form.constructionDept" placeholder="请输入施工单位" maxlength="30" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="建筑物类型" prop="buildingTypeId">
              <treeselect v-model="form.buildingTypeId" :options="buildingTypeOptions" :show-count="true"
                placeholder="请选择建筑物类型" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="项目图片" prop="projectImages">
          <image-upload v-model="form.projectImages" :limit="3" />
        </el-form-item>
      </el-form>
      <el-divider></el-divider>
      <el-form ref="form2" :model="form2" :rules="rules" label-width="88px">
        <el-form-item label="审核结果" prop="status">
          <el-radio-group v-model="form2.status" size="medium">
            <el-radio label="1">通过</el-radio>
            <el-radio label="0">驳回</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--地图显示-->
    <el-dialog :visible.sync="mapOpen" fullscreen append-to-body>
      <div>
        <AMap :position="position" />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProject, getProject, delProject, addProject, updateProject, approveProject } from "@/api/projectManage/project";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { treeBuildingType } from "@/api/building/BuildingType";
import { allCompany } from "@/api/company/company";
import AMap from '@/components/Amap/AMap';

export default {
  name: "Project",
  dicts: ['approval_status', 'project_kind_id', "structure_type_id", "sys_valid"],
  components: { Treeselect, AMap },
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目表格数据
      projectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      daterangeCreatedDate: [],
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        name: null,
        projectKindId: null,
        buildingTypeId: null,
        structureTypeId: null,
        buildingArea: null,
        schedule: null,
        classproject: null,
        recycled: null,
        location: null,
        areaId: null,
        address: null,
        personInCharge: null,
        phone: null,
        buildingDept: null,
        constructionDept: null,
        supervisorDept: null,
        state: null,
        transportCompanyId: null,
        startWorkData: null,
        beCompletedData: null,
        belongToStreet: null,
        approvalStatus: null,
        valid: null,
        resourceState: null,
        resourceExamineTime: null,
        resourceExplain: null,
        resourceAppliedTime: null,
        resourceUnit: null,
        resourceContractNo: null,
        resourcePlanFileId: null,
        resourceContractFileId: null,
        resourceApplyContractFileId: null,
      },
      // 表单参数
      form: {},
      form2: {},
      // 表单校验
      rules: {
        status: [
          { required: true, message: "审核结果不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "项目名称不能为空", trigger: "blur" }
        ],
        buildingArea: [
          { required: true, message: "建筑面积不能为空", trigger: "blur" }
        ],
        personInCharge: [
          { required: true, message: "项目负责人不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "电话不能为空", trigger: "blur" },
          {
            pattern: this.$ConstantUtil.REG_PHONE,
            message: "请输入正确的手机号码",
            trigger: ["blur", "change"]
          }
        ],
      },
      // 清运公司选项列表
      transportOptions: [],
      // 建筑物类型选项树
      buildingTypeOptions: undefined,
      // 是否地图显示弹出层
      mapOpen: false,
      // 项目坐标
      position: [],
    };
  },
  created () {
    this.getList();
    this.getTreeBuildingType();
    this.getTransportCompanyList();
  },
  methods: {
    // 建筑物类型选项树
    getTreeBuildingType () {
      treeBuildingType().then(response => {
        this.buildingTypeOptions = response.data;
      });
    },
    // 清运公司选项列表
    getTransportCompanyList () {
      allCompany().then(response => {
        this.transportOptions = response.data;
      })
    },
    /** 查询项目列表 */
    getList () {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreatedDate && '' != this.daterangeCreatedDate) {
        this.queryParams.params["beginCreatedDate"] = this.daterangeCreatedDate[0];
        this.queryParams.params["endCreatedDate"] = this.daterangeCreatedDate[1];
      }
      this.queryParams.approvalStatus = 10;
      listProject(this.queryParams).then(response => {
        this.projectList = response.records;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        id: null,
        name: null,
        projectKindId: null,
        buildingTypeId: null,
        structureTypeId: null,
        buildingArea: null,
        schedule: null,
        classproject: null,
        recycled: null,
        location: null,
        areaId: null,
        address: null,
        personInCharge: null,
        phone: null,
        buildingDept: null,
        constructionDept: null,
        supervisorDept: null,
        state: null,
        transportCompanyId: null,
        startWorkData: null,
        beCompletedData: null,
        belongToStreet: null,
        approvalStatus: null,
        valid: null,
        resourceState: null,
        resourceExamineTime: null,
        resourceExplain: null,
        resourceAppliedTime: null,
        resourceUnit: null,
        resourceContractNo: null,
        resourcePlanFileId: null,
        resourceContractFileId: null,
        resourceApplyContractFileId: null,
        createdDate: null,
        modifiedDate: null,
        projectImages: null
      };
      this.form2 = { status: null };
      this.resetForm("form");
      this.resetForm("form2");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.daterangeCreatedDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加项目";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const id = row.id || this.ids
      getProject(id).then(response => {
        this.form = response.data;
        this.position = response.data.location.coordinates;
        this.open = true;
        this.title = "修改项目";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form2"].validate(valid => {
        if (valid) {
          approveProject({ id: this.form.id, status: this.form2.status }).then(response => {
            this.$modal.msgSuccess("操作成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除选中的数据项？').then(function () {
        return delProject(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download('projectManage/project/export', {
        ...this.queryParams
      }, `项目管理_${new Date().getTime()}.xlsx`)
    },
    // 定位
    handleLocation (row) {
      this.position = row.location.coordinates;
      this.mapOpen = true;
    },
    handleApprove (row) {
      this.reset();
      const id = row.id || this.ids
      getProject(id).then(response => {
        this.form = response.data;
        this.position = response.data.location.coordinates;
        this.open = true;
        this.title = "项目审批";
      });
    }
  }
};
</script>

<style scoped>
#lmap {
  height: 30vh;
  width: 30vw;
}
</style>